<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar"
          @click="selectimageFn"
          :src="profile.photo"/>

          <input type="file"  hidden  ref="photo"   @change="uploadphotoFn"     >
        </template>
      </van-cell>
      <van-cell title="名称" is-link
        @click="show=true"
       :value="profile.name" />
<van-dialog v-model="show" title="修改" show-cancel-button  :beforeClose='onbeforeCloseFn'  >
<van-field v-model.trim="value2"  placeholder="请输入用户名" v-focus    />
</van-dialog>
      <van-cell title="性别" is-link   :value="profile.gender===1?  '男' :'女' "/>
      <van-cell title="生日" is-link  :value="profile.birthday"     @click="isShowBirth=true"    />
      <!-- 修改时间 -->
<van-popup v-model="isShowBirth" position="bottom" style="height: 50%" round>
      <!-- 日期选择控件 -->
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择出生日期"
        :min-date="minDate"
        :max-date="maxDate"
        :show-toolbar="true"
        @confirm='onDatafiermFn'
        @cancel='onDateCancefn'
      />
</van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { currentData } from '@/utils/data'
import { getUserProfileAPI, uploadPhotoAPI, updateUsrprofileAPI } from '@/api/user'
export default {
  name: 'UserEdit',
  data () {
    return {
      profile: {}, // 用户基本资料
      show: false, // 名称对话框显示
      value2: '', // 名称变量

      isShowBirth: false, // 显示时间选择器
      minDate: new Date(1900, 0, 1), // 最小的可选的日期
      maxDate: new Date(), // 最大的可选日期
      currentDate: new Date() // 当前日期

    }
  },

  async  created () {
    const res = await getUserProfileAPI({

    })
    this.profile = res.data
    this.currentDate = new Date(res.data.birthday)
    this.value2 = res.data.name
  },
  methods: {
    // 点击图片事件
    selectimageFn () {
      this.$refs.photo.click()
    },

    async  uploadphotoFn (e) {
      const { 0: file } = e.target.files

      //  判断
      if (!file) return
      const { name, size } = file
      if (!/\.(png|jpe?g|webp|svg|gif)$/i.test(name)) {
        this.$notify({ type: 'danger', message: '图片格式不对' })
        return
      }
      if (size / 1024 / 1024 > 2) {
        this.$notify({ type: 'danger', message: '图片大了' })
        return
      }
      // 组装符合后台的formdata格式
      const formData = new FormData()

      formData.append('photo', file)

      const res = await uploadPhotoAPI({
        formData
      })
      // 图片地址从新赋值
      this.profile.photo = res.data.photo
      // 缓存头像不变
      this.$store.commit('user/setUser', {
        photo: res.data.photo
      })
    },
    // 修改名称回调函数   组件库提供的
    async  onbeforeCloseFn (action, done) {
      // 确认
      if (action === 'confirm') {
        // 费控校验
        if (!this.value2) {
          this.$notify({ type: 'danger', message: '不能为空' })
          // 正则校验
          done(false) // 关闭弹窗
        } else if (!/^[a-z\d\u4e00-\u9fa5]{1,7}$/i.test(this.value2)) {
          this.$notify({ type: 'danger', message: '1-7位' })
          done(false) // 关闭弹窗
          // 掉接口
        } else {
          if (this.value2 === this.profile.name) {
            done()
            return
          }
          await updateUsrprofileAPI({
            birthday: this.profile.birthday,
            name: this.value2

          })

          // 关闭弹窗
          done()
          this.profile.name = this.value2
          this.$store.commit('user/setUser', {
            name: this.value2
          })
        }
      } else {
        this.value2 = this.profile.name
        done()
      }
    },
    // 确认
    async onDatafiermFn () {
      // 少发请求
      if (this.profile.birthday === currentData(this.currentDate)) {
        // 关
        this.isShowBirth = false
        return
      }

      await updateUsrprofileAPI({
        birthday: currentData(this.currentDate),
        name: this.profile.name
      })

      // vue赋值
      this.profile.birthday = currentData(this.currentDate)
      // 关
      this.isShowBirth = false
    },
    // 取消
    onDateCancefn () {
      this.isShowBirth = false
      this.currentDate = new Date(this.profile.birthday)
    }
  }

}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
